package com.chalmers.irc2you;

import android.app.Dialog;
import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.chalmers.irc2you.model.DataHandler;
import com.chalmers.irc2you.network.Connection;

public class MainActivity extends TabActivity {

	private DataHandler dh;
	private Intent intent; // Reusable Intent

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Resources res = getResources(); // Resource object to get Drawables
		TabHost tabHost = getTabHost(); // The activity TabHost
		TabHost.TabSpec spec; // Resusable TabSpec for each tab

		dh = DataHandler.getInstance(getApplicationContext());
		dh.loadChannels();
		Connection.getInstance(getApplicationContext());

		// Create an Intent to launch an Activity for the tab (to be reused)
		intent = new Intent().setClass(this, MainTab.class);

		// Initialize a TabSpec for each tab and add it to the TabHost
		spec = tabHost.newTabSpec("channels").setIndicator("Channels",
				res.getDrawable(R.drawable.ic_menu_tags)).setContent(intent);
		tabHost.addTab(spec);

		// Do the same for the other tabs
		intent = new Intent().setClass(this, ChannelTab.class);
		spec = tabHost.newTabSpec("currentchannel").setIndicator(
				"Current Channel", res.getDrawable(R.drawable.ic_menu_tag))
				.setContent(intent);
		tabHost.addTab(spec);

		tabHost.setCurrentTab(0);
	}

	public void onResume() {
		dh.loadChannels();
		super.onResume();
	}

	public void onPause() {
		dh.saveChannels();
		super.onPause();
	}

	public void onDestroy() {
		dh.saveChannels();
		super.onDestroy();
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainmenu, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.preferencesitem:
			Intent intent = new Intent(MainActivity.this, SettingsTab.class);
			startActivity(intent);
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}
}
